<?php

namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class MyController extends BaseController
{
    public function my(Request $request)
    {
        return view('home/my');
    }
    //修改密码
    public function editPassword(Request $request)
    {
        if($request->isMethod('post')){
            $this->validate($request,[
                'old_password'=>['required',function($attribute, $value, $fail){
                   $pwd=DB::table('user')->where('user_id',auth()->user()->user_id)->value('password');
                   if(!Hash::check($value,$pwd)){
                       return $fail('旧密码不正确');
                   }
                }],
                'phone'=>'required|exists:user',
                'yzm'=>'required|timeout|yzmgz',
                'password'=>'required|min:6|confirmed',
            ],[
                'old_password.required'=>'旧密码不能为空',
                'phone.required'=>'手机号不能为空',
                'phone.exits'=>'手机号不存在',
                'yzm.required'=>'验证码不能为空',
                'yzm.timeout'=>'验证码已过期',
                'yzm.yzmgz'=>'验证码不正确',
                'password.required'=>'新密码不能为空',
                'password.min'=>'密码最小为6位',
                'password.confirmed'=>'两次密码不一致'
            ]);
            $uid=auth()->user()->user_id;
            $password=$request->password;
            $res=DB::table('user')->where('user_id',$uid)->update([
                'password'=>Hash::make($password)
            ]);
            if($res){
                auth()->logout();
                return $this->success('修改成功');
            }else{
                return $this->error('修改失败');
            }
        }else{
            return view('home/password_edit');
        }
    }
    //设置交易所账号
    public function setAccount(Request $request)
    {
        if($request->isMethod('post')){
            $this->validate($request,[
                'ex_id'=>'required',
                'username.0'=>'required',
                'AccessKey.0'=>'required',
                'SecretKey.0'=>'required',
                'pay_password.0'=>'required',
            ],[
                'ex_id.required'=>'请选择交易所',
                'username.0.required'=>'账号不能为空',
                'AccessKey.0.required'=>'AccessKey不能为空',
                'SecretKey.0.required'=>'SecretKey不能为空',
                'pay_password.0.required'=>'支付密码不能为空'
            ]);
            $uid=auth()->user()->user_id;
            $data=$request->all();
            $list=DB::table('ex_username')->where(['user_id'=>$uid,'ex_id'=>$data['ex_id']])->get();
            if(count($list)>1){
                return back()->withErrors('已设置过账号');
            }else if(count($list)==1 && count($data['username']>1)){
                return back()->withErrors('已设置过一个账号,只能再设置一个账号');
            }
            unset($data['_token']);
            if(!empty($data['username'][0]) && !empty($data['username'][1])){
                $res=DB::table('ex_username')->insert([
                    [
                        'ex_id'=>$data['ex_id'],
                        'user_id'=>$uid,
                        'username'=>$data['username'][0],
                        'AccessKey'=>$data['AccessKey'][0],
                        'SecretKey'=>$data['SecretKey'][0],
                        'pay_password'=>Hash::make($data['pay_password'][0]),
                        'created_at'=>now()
                    ],
                    [
                        'ex_id'=>$data['ex_id'],
                        'user_id'=>$uid,
                        'username'=>$data['username'][1],
                        'AccessKey'=>$data['AccessKey'][1],
                        'SecretKey'=>$data['SecretKey'][1],
                        'pay_password'=>Hash::make($data['pay_password'][1]),
                        'created_at'=>now()
                    ]
                ]);
                if($res){
                    return back()->with('success','添加成功');
                }else{
                    return back()->withErrors('添加失败');
                }
            }else{
                $data['user_id']=$uid;
                $res=DB::table('ex_username')->insert([
                    'ex_id'=>$data['ex_id'],
                    'user_id'=>$uid,
                    'username'=>$data['username'][0],
                    'AccessKey'=>$data['AccessKey'][0],
                    'SecretKey'=>$data['SecretKey'][0],
                    'pay_password'=>Hash::make($data['pay_password'][0]),
                    'created_at'=>now()
                ]);
                if($res){
                    return back()->with('success','添加成功');
                }else{
                    return back()->withErrors('添加失败');
                }
            }

        }else{
            $list=DB::table('exchange')->get();
            return view('home/account-setting',compact('list'));
        }

    }
    //查询是否设置账号
    public function isSetAccount($id)
    {
        $uid=auth()->user()->user_id;
        $list=DB::table('ex_username')->where(['user_id'=>$uid,'ex_id'=>$id])->get();
        $count=count($list);
        if($count>1){
           return response([
               'state'=>2
           ]);
        }elseif ($count==1){
            return response([
                'state'=>1
            ]);
        }else{
            return response([
                'state'=>0
            ]);
        }


    }
    //币种设置
    public function currSet(Request $request)
    {
        if($request->isMethod('post')){
            $this->validate($request,[
                'ex_id'=>'required',
                'coin_name'=>'required',
                'account_id'=>'required',
                'param1'=>'required',
                'param2'=>'required',
                'param3'=>'required',
                'param4'=>'required',
                'param5'=>'required',
                'param6'=>'required',
                'param7'=>'required',
                'param8'=>'required',
                'param9'=>'required',
                'param10'=>'required',
                'param11'=>'required',
                'yzm'=>'required|timeout|yzmgz'
            ],[
                'ex_id.required'=>'请选择交易所',
                'coin_name.required'=>'请选择币种',
                'account_id.required'=>'请选择账户',
                'param1.required'=>'参数1不能为空',
                'param2.required'=>'参数2不能为空',
                'param3.required'=>'参数3不能为空',
                'param4.required'=>'参数4不能为空',
                'param5.required'=>'参数5不能为空',
                'param6.required'=>'参数6不能为空',
                'param7.required'=>'参数7不能为空',
                'param8.required'=>'参数8不能为空',
                'param9.required'=>'参数9不能为空',
                'param10.required'=>'参数10不能为空',
                'param11.required'=>'参数11不能为空',
                'yzm.required'=>'验证码不能为空',
                'yzm.timeout'=>'验证码已过期',
                'yzm.yzmgz'=>'验证码不正确'
            ]);
            $data=$request->all();
            unset($data['token']);
            unset($data['yzm']);
            $uid=auth()->user()->user_id;
            $data['user_id']=$uid;
            $data['coin_id']=$this->coin_id($request->coin_name);
            $first=DB::table('coin_conf')->where(['user_id'=>$uid,'coin_id'=>$data['coin_id'],'account_id'=>$data['account_id'],'coin_name'=>$data['coin_name']])->first();
            if(!empty($first)){
               $up=DB::table('coin_conf')->where(['user_id'=>$uid,'coin_id'=>$data['coin_id'],'account_id'=>$data['account_id'],'coin_name'=>$data['coin_name']])
                    ->update([
                        'param1'=>$data['param1'],
                        'param2'=>$data['param2'],
                        'param3'=>$data['param3'],
                        'param4'=>$data['param4'],
                        'param5'=>$data['param5'],
                        'param6'=>$data['param6'],
                        'param7'=>$data['param7'],
                        'param8'=>$data['param8'],
                        'param9'=>$data['param9'],
                        'param10'=>$data['param10'],
                        'param11'=>$data['param11'],
                    ]);
               if($up){
                   return $this->success('修改成功');
               }else{
                   return $this->error('修改失败');
               }
            }else{
                $res=DB::table('coin_conf')->insert($data);
                if($res){
                    return $this->success('添加成功');
                }else{
                    return $this->error('添加失败');
                }
            }
        }else{
            $phone=auth()->user()->phone;
            $list=DB::table('exchange')->get();
            $curr=DB::table('exchange')->value('currency');
            if(strpos($curr,',')){
                $currency=explode(',',$curr);
            }else{
                $currency=[$curr];
            }
            return view('home/currency-setting',compact('phone','list','currency'));
        }

    }
    //根据交易所查询币种
    public function selectCurr(Request $request)
    {
        $ex_id=$request->ex_id;
        if(!empty($ex_id)){
            $curr=DB::table('exchange')->where('ex_id',$ex_id)->value('currency');
            if(strpos($curr,',')){
               $currency=explode(',',$curr);
            }else{
                $currency=[$curr];
            }
        }else{
            $currency=[];
        }
        return $this->success('',$currency);
    }
    //我的邀请码
    public function code()
    {
        $uid=auth()->user()->user_id;
        $code=DB::table('user')->where('user_id',$uid)->value('code');
        return view('home/my-agent',compact('code'));
    }
    //退款记录
    public function refund(Request $request)
    {
        $uid=auth()->user()->user_id;
        $data=DB::table('cash')->where('user_id',$uid)->where('type',2)->get();
        return view('home/refund-record',compact('data'));
    }
    //预付金支付
    public function pay(Request $request)
    {

        return view('home/prepaid');
    }
    //查看资产盈亏状态
    public function profitLoss(Request $request)
    {
        $list=DB::table('exchange')->get();
        return view('home/profit-loss',compact('list'));

    }

    //查询账号
    public function selUsername(Request $request)
    {
        $uid=auth()->user()->user_id;
        $ex=$request->ex_id;
        $list=DB::table('ex_username')->where(['user_id'=>$uid,'ex_id'=>$ex])->get();
        return $this->success('',$list);

    }
    //查询币种参数
    public function configParam(Request $request)
    {
        $ex_id=$request->ex_id;
        $coin_name=$request->coin_name;
        $account_id=$request->account_id;
        $uid=auth()->user()->user_id;
        $info=DB::table('coin_conf')->where(['ex_id'=>$ex_id,'coin_name'=>$coin_name,'account_id'=>$account_id,'user_id'=>$uid])->first();
        return $this->success('',$info);
    }
    //查询账户余额
    public function currBalance(Request $request)
    {
        $uid=auth()->user()->user_id;
        $ex_id=$request->ex_id;
        $acc_id=$request->account_id;
        $list=DB::table('coin_conf')->where(['ex_id'=>$ex_id,'user_id'=>$uid,'account_id'=>$acc_id])->get();
        return $this->success('',$list);

    }

    public function coin_id($coin_id){
        return array_search($coin_id,['BTC','ETH','LTC','DASH','ZEC','XRP','EOS','BCH']);
    }

}
